import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
import axios from 'axios'
export default new Vuex.Store({
  // 相当于组件的data
  state: {
    counter: NaN
  },
  // 相当于组件的computed
  getters: {
    douCounter(state) {
      return state.counter * 2
    }
  },
  // 相当于组件methods
  // 必须是同步的
  mutations: {
    SET_COUNTER(state, payload) {
      state.counter = payload
    }
  },
  // 相当于组件的methods
  // 支持异步
  actions: {
    async GET_COUNTER(context) {
      const resp = await axios.get('/api/counter')
      context.commit('SET_COUNTER', resp.data.data)
    }
  },
  modules: {
  }
})
